package com.xy.test;

import com.xy.config.MainConfigOfProfile;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Profile;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.sql.DataSource;
import java.sql.*;

/**
 * @author 谢阳
 * @version 1.8.0_131
 * @date 2022/4/9 8:44
 * @description
 */

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = MainConfigOfProfile.class)
@ActiveProfiles("dev")
public class IOCTestDataSource {
    @Autowired ApplicationContext context;

    @Test
    public void testData() {
        /*// 1、创建applicationContext
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();

        // 2、设置需要激活的环境
        context.getEnvironment().setActiveProfiles("dev");

        // 3、注册主配置类
        context.register(MainConfigOfProfile.class);

        // 4、启动刷新容器
        context.refresh();*/

        String[] names = context.getBeanDefinitionNames();
        for (String name : names) {
            System.out.println("name = " + name);
        }

    }

    @Test
    public void testDataSource(){
        //Connection connection = dataSource.getConnection();
        //PreparedStatement statement = connection.prepareStatement("select * from t_test");
        //ResultSet resultSet = statement.executeQuery();
        //ResultSetMetaData metaData = statement.getMetaData();
        //int columnCount = metaData.getColumnCount();
        //while (resultSet.next()) {
        //    for (int i = 0; i < columnCount; i++) {
        //        Object value = resultSet.getObject(i + 1);
        //        String name = metaData.getColumnLabel(i + 1);
        //        System.out.print(name + "="  + value + "\t");
        //    }
        //    System.out.println();
        //
        //}
    }

}
